import os

from utils import date_util, k_util, ave_util, excel_util, json_util, read_config
from service import deal_service, record_service, trigger_service, score_service

today_str = date_util.get_format_date_str("%Y-%m-%d")
result_path = os.path.join(read_config.data_path, 'result', '突破均线', today_str, '历史验证')
result_file_name = '验证结果'
view_file_path = os.path.join(result_path, '验证')

if not os.path.exists(view_file_path):
    os.makedirs(view_file_path)  # 没有创建文件夹


# list_s : 股票列表
# size : 要验证的股票交易日天数
def monitor_up_down(list_s, size):
    # 循环要监控的列表
    for stock_info in list_s:
        code = stock_info['code']  # 股票编码
        name = stock_info['name']  # 股票名称
        monitor_stock(code, name, size)  # 验证股票


# 验证当前股票历史
def monitor_stock(code, name, size):
    capital_pool = 1000000  # 百万的初始资金
    position_money = 0  # 持仓金额
    hand_num = 0  # 持有多少手股票
    k_lines = k_util.day_k_local(code, size + 10)  # 获取比size多五日的k线数据 k_util 可以在代码中获取
    k_lines.reverse()
    size = len(k_lines)  # 重新定义size防止新股票不够用
    k_lines_five = k_lines[5:10]  # 五日K线列表
    k_lines_ten = k_lines[0:10]  # 十日K线列表
    pointer5 = 0  # 5日均线k_lines_five替换指针
    pointer10 = 0  # 10日均线k_lines_ten替换指针
    ave_five = ave_util.ave_line_price(k_lines_five, 5)
    ave_ten = ave_util.ave_line_price(k_lines_ten, 10)
    sp4 = float(json_util.info_to_json(k_lines_five[4])['sp'])
    flag5 = 1  # 1:监控突破 -1:监控跌破 初始化
    flag10 = 1  # 1:监控突破 -1:监控跌破 初始化
    if sp4 > ave_five: flag5 = -1
    if sp4 > ave_ten: flag10 = -1
    # 生成的excel标题行
    content_arr = [[]]
    common_data = {'code': code, 'name': name, 'capital': capital_pool, 'position': position_money, 'hand': hand_num,
                   'flag5': flag5, 'flag10': flag10, 'trigger_num': 0, 'content': content_arr}
    for num in range(size - 10):
        current_k = json_util.info_to_json(k_lines[num + 10])  # 对比日k线
        current_rq = current_k['rq']  # 当前日的日期yyyy-MM-dd
        rt_list = get_rt_price_list(current_k)
        for rt_price in rt_list:
            if current_rq == '2021-01-11' and rt_price < 15.9:
                print('data[]')
            ave_five = ave_util.ave_price_rt(k_lines_five, rt_price)  # 五日均线价格
            ave_ten = ave_util.ave_price_rt(k_lines_ten, rt_price)  # 十日均线价格
            common_data['rq'] = current_rq  # 对比日日期yyyy-MM-dd
            common_data['ave5'] = ave_five  # 五日均线价格
            common_data['ave10'] = ave_ten  # 十日均线价格
            common_data['rt_price'] = rt_price  # 十日均线价格
            common_data = score_service.common_exam(common_data)  # 处理当前技术形态评分
            common_data = trigger_service.deal_trigger(common_data)  # 处理触发的情况
            common_data = deal_service.deal_complete(common_data)  # 完成交易
            common_data = record_service.trans_excel_format(common_data)  # 记录结果
        k_lines_five[pointer5] = current_k  # 用当日替换最老的一天的k线
        k_lines_ten[pointer10] = current_k  # 用当日替换最老的一天的k线
        pointer5 = (pointer5 + 1) % 5  # 更新替换指针
        pointer10 = (pointer10 + 1) % 10  # 更新替换指针
    if len(common_data['content']) > 1:
        common_data = record_service.deal_excel_result(common_data)
        excel_util.gen_an_excel(view_file_path, name + '--' + code, '', common_data['content'])


# 虚拟出当日的价格走势
# 变化方法
# 1. 开盘向收盘变化
# 2. 高的向高的变 低的向低的变
def get_rt_price_list(cur_k):
    rt_list = []
    min = float(cur_k['min'])
    max = float(cur_k['max'])
    mid = round((min + max) / 2, 2)
    rt_list.append(mid)
    cycle_num = int((max - min) * 50)
    if cycle_num == 0:
        return rt_list
    if cycle_num > 50:
        cycle_num = 50
        step_long = round((max - min) / 50, 4)
    else:
        step_long = round((max - min) / cycle_num, 4)
    for i in range(1, int(cycle_num / 2)):
        rt_list.append(round(mid + step_long * i, 2))
        rt_list.append(round(mid - step_long * i, 2))
    return rt_list


if __name__ == '__main__':
    stock_list = [
        # {'code': '600660', 'name': '福耀玻璃'},
        # {'name': '王府井', 'code': '600859'},
        {'code': '601318', 'name': '中国平安'},
        # {'name': '中远海控', 'code': '601919', 'exchange': '1'},
    ]
    monitor_up_down(stock_list, 60)
